#########################################################################
## Replication file for                                           
## "Structuring intra-party politics: a mixed-method study of ideological 
##    and hierarchical factions in parties"                 
##                                                    
## Authors: Ann-Kristin Kölln (ann-kristin.kolln@gu.se) and                                                  
##  		Jonathan Polk (jonathan.polk@svet.lu.se)
##              
## 28th June 2023
#########################################################################


# R version 4.3.0 (2023-04-21) -- "Already Tomorrow"; Platform: x86_64-apple-darwin20 (64-bit)

rm(list = ls())


# load data file 
dat3<-read.csv("PartyMembershipSurvey.csv", header =TRUE)


## install and load needed packages
install.packages(c("tidyr", "NbClust"))

library(tidyr)   # version 1.3.0
library(NbClust) # version 3.0.1



##### GENERATING INPUT FOR TABLE D1 #####


### LEFT-RIGHT

## Social Democrats

#slice data and only use data for S
S19<-dat3[dat3$partiabb=="S",]

set.seed(3)

S19<-S19 %>% drop_na(iself)
S19_iself<-S19[c("iself")]


##applying stopping rule Duda/Hart
duda_S<-NbClust(data = S19_iself,  distance = "euclidean", method="ward.D2", min.nc = 2, max.nc = 10, index= "duda")  

duda_S$Best.nc



## Moderates

#slice data and only use data for M
M19<-dat3[dat3$partiabb=="M",]

set.seed(3)

M19<-M19 %>% drop_na(iself)
M19_iself<-M19[c("iself")]


##applying stopping rule Duda/Hart
duda_M<-NbClust(data = M19_iself,  distance = "euclidean", method="ward.D2", min.nc = 2, max.nc = 10, index= "duda")  

duda_M$Best.nc


## Green Party

#slice data and only use data for Mp
Mp19<-dat3[dat3$partiabb=="Mp",]

set.seed(3)

Mp19<-Mp19 %>% drop_na(iself)
Mp19_iself<-Mp19[c("iself")]


##applying stopping rule Duda/Hart
duda_Mp<-NbClust(data = Mp19_iself,  distance = "euclidean", method="ward.D2", min.nc = 2, max.nc = 10, index= "duda")  

duda_Mp$Best.nc



## Left Party

#slice data and only use data for Mp
V19<-dat3[dat3$partiabb=="V",]

set.seed(3)

V19<-V19 %>% drop_na(iself)
V19_iself<-V19[c("iself")]


##applying stopping rule Duda/Hart
duda_V<-NbClust(data = V19_iself,  distance = "euclidean", method="ward.D2", min.nc = 2, max.nc = 10, index= "duda")  

duda_V$Best.nc



## Christian Democrats

#slice data and only use data for KD
KD19<-dat3[dat3$partiabb=="KD",]

set.seed(3)

KD19<-KD19 %>% drop_na(iself)
KD19_iself<-KD19[c("iself")]


##applying stopping rule Duda/Hart
duda_KD<-NbClust(data = KD19_iself,  distance = "euclidean", method="ward.D2", min.nc = 2, max.nc = 10, index= "duda")  

duda_KD$Best.nc



## Liberal Party

#slice data and only use data for Fp
L19<-dat3[dat3$partiabb=="L",]

set.seed(3)

L19<-L19 %>% drop_na(iself)
L19_iself<-L19[c("iself")]


##applying stopping rule Duda/Hart
duda_L<-NbClust(data = L19_iself,  distance = "euclidean", method="ward.D2", min.nc = 2, max.nc = 10, index= "duda")  

duda_L$Best.nc



## Center Party 

#slice data and only use data for Fi and drop NA's for self-placement
C19<-dat3[dat3$partiabb=="C",]

set.seed(3)

C19<-C19 %>% drop_na(iself)
C19_iself<-C19[c("iself")]


##applying stopping rule Duda/Hart
duda_C<-NbClust(data = C19_iself,  distance = "euclidean", method="ward.D2", min.nc = 2, max.nc = 10, index= "duda")  

duda_C$Best.nc




### GAL-TAN ###

## Social Democrats 

#slice data and only use data for S
S19<-dat3[dat3$partiabb=="S",]

set.seed(3)

library(tidyr)
S19<-S19 %>% drop_na(iself_c)
S19_iselfc<-S19[c("iself_c")]

##applying stopping rule Duda/Hart
duda_S_c<-NbClust(data = S19_iselfc,  distance = "euclidean", method="ward.D2", min.nc = 2, max.nc = 10, index= "duda")  

duda_S_c$Best.nc



## Moderates 

#slice data and only use data for M
M19<-dat3[dat3$partiabb=="M",]

set.seed(3)

M19<-M19 %>% drop_na(iself_c)
M19_iselfc<-M19[c("iself_c")]


##applying stopping rule Duda/Hart
duda_M_c<-NbClust(data = M19_iselfc,  distance = "euclidean", method="ward.D2", min.nc = 2, max.nc = 10, index= "duda")  

duda_M_c$Best.nc


## Green Party 

#slice data and only use data for Mp
Mp19<-dat3[dat3$partiabb=="Mp",]

set.seed(3)

Mp19<-Mp19 %>% drop_na(iself_c)
Mp19_iselfc<-Mp19[c("iself_c")]


##applying stopping rule Duda/Hart
duda_Mp_c<-NbClust(data = Mp19_iselfc,  distance = "euclidean", method="ward.D2", min.nc = 2, max.nc = 10, index= "duda")  

duda_Mp_c$Best.nc



## Left Party

#slice data and only use data for Mp
V19<-dat3[dat3$partiabb=="V",]

set.seed(3)

V19<-V19 %>% drop_na(iself_c)
V19_iselfc<-V19[c("iself_c")]


##applying stopping rule Duda/Hart
duda_V_c<-NbClust(data = V19_iselfc,  distance = "euclidean", method="ward.D2", min.nc = 2, max.nc = 10, index= "duda")  

duda_V_c$Best.nc


## Christian Democrats 

#slice data and only use data for KD
KD19<-dat3[dat3$partiabb=="KD",]

set.seed(3)

KD19<-KD19 %>% drop_na(iself_c)
KD19_iselfc<-KD19[c("iself_c")]


##applying stopping rule Duda/Hart
duda_KD_c<-NbClust(data = KD19_iselfc,  distance = "euclidean", method="ward.D2", min.nc = 1, max.nc = 10, index= "duda")  # only converges if min set to 1 because that's the optimal solution

duda_KD_c$Best.nc



## Liberal Party 

#slice data and only use data for L
L19<-dat3[dat3$partiabb=="L",]

set.seed(3)

L19<-L19 %>% drop_na(iself_c)
L19_iselfc<-L19[c("iself_c")]


##applying stopping rule Duda/Hart
duda_L_c<-NbClust(data = L19_iselfc,  distance = "euclidean", method="ward.D2", min.nc = 1, max.nc = 10, index= "duda")  # only converges if min set to 1 because that's the optimal solution

duda_L_c$Best.nc




## Center Party 

#slice data and only use data for C
C19<-dat3[dat3$partiabb=="C",]

set.seed(3)

C19<-C19 %>% drop_na(iself_c)
C19_iselfc<-C19[c("iself_c")]


##applying stopping rule Duda/Hart
duda_C_c<-NbClust(data = C19_iselfc,  distance = "euclidean", method="ward.D2", min.nc = 1, max.nc = 10, index= "duda")  # only converges if min set to 1 because that's the optimal solution


duda_C_c$Best.nc

